package hra;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

class Core implements ICore
{
	private int pocetHracu = 0;
	private int pocetAI = 0;
	private IGui gui = null;
	private String[] uzivatele = null;
	List<player> hraci = new ArrayList<player>();
	private int max = 100;
	private int current = 0;
	private Random rnd = new Random();

	@Override
	public void setUzivatelePc(String[] str) // set PC users (probably names of
												// playing users)
	{
		uzivatele = str;
	}

	@Override
	public void setPocetAI(int num) // set number of AIs
	{
		pocetAI = num;
	}

	@Override
	public void hraj() // play
	{
		if (!check())
		{
			gui.chyba2();
		}
		for (String str : uzivatele)
			hraci.add(new player(str, typ.Clovek));

		if (hraci.size() != pocetHracu)
		{
			gui.chyba();
			return;
		}
		if (pocetAI > 0)
			for (int i = 1; i <= pocetAI; i++)
				hraci.add(new player("Pocitac" + i, typ.AI));

		while (current < max)
		{
			for (player hrac : hraci)
			{
				if (current < max)
				{
					gui.hracNaTahu(hrac);
					if (hrac.getTyp() == typ.Clovek)
						current = current + hrac.tah();
					else
						current = current + (rnd.nextInt(8) + 1);
					gui.meziVysledek(current);
				}
				else
				{
					gui.prohra(hraci.get(hraci.indexOf(hrac) - 1));
					return;
				}
			}
		}
		gui.prohra(hraci.get(hraci.size() - 1));
	}

	private boolean check()
	{
		if (pocetAI == 1 && pocetHracu == 1)
			return true;
		if (pocetHracu < 2 && pocetAI < 2)
			return false;
		return true;
	}

	@Override
	public void setPocetHracu(int num) // set number of players
	{
		pocetHracu = num;
	}

	@Override
	public void nastavOdkazNaGui(IGui Gui) // set pointer to GUI
	{
		gui = Gui;
	}

	@Override
	public void setMax(int Max)
	{
		max = Max;
	}
}